package com.pzhuweb.sgblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhuweb.sgblog.domain.dto.CategoryDto;
import com.pzhuweb.sgblog.domain.entity.Article;
import com.pzhuweb.sgblog.domain.entity.Category;
import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.domain.vo.PageVo;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.mapper.CategoryMapper;
import com.pzhuweb.sgblog.service.ArticleService;
import com.pzhuweb.sgblog.service.CategoryService;
import com.pzhuweb.sgblog.domain.utils.BeanCopyUtils;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import com.pzhuweb.sgblog.domain.utils.SystemConstants;
import com.pzhuweb.sgblog.domain.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Override
    public ResponseResult<?> getCategoryList() {

        // 查询分类列表，封装成 ResponseResult 返回
        LambdaQueryWrapper<Article> queryWrapperArticle = new LambdaQueryWrapper<>();
        // 查询文章表,状态为已发布的文章
        queryWrapperArticle.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(queryWrapperArticle);
        // lambda表达式
        Set<Long> categoryIds = articleList.stream().map(Article::getCategoryId).collect(Collectors.toSet());
        // 分类列表要求只查询有文章的
        List<Category> categories = listByIds(categoryIds);

        // 分类列表必须是正常的(没有被禁用)
        List<Category> categoriesList = categories.stream().filter(category -> category.getStatus().equals(SystemConstants.ARTICLE_STATUS_NORMAL_STR)).collect(Collectors.toList());
        // BeanCopy
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoriesList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult<?> listAllCategory() {
        // 查询所有分类
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult<?> getPageList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), Category::getName, name)
                .eq(StringUtils.hasText(status), Category::getStatus, status);
        Page<Category> categoryPage = new Page<>(pageNum, pageSize);
        page(categoryPage,queryWrapper);
        PageVo pageVo = new PageVo(categoryPage.getRecords(),categoryPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<?> addCategory(CategoryDto categoryDto) {
        Category category = new Category();
        category.setName(categoryDto.getName());
        category.setDescription(categoryDto.getDescription());
        category.setStatus(categoryDto.getStatus());
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> getDetails(Long id) {
        Category category = getById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @Override
    public ResponseResult<?> updateCategory(CategoryDto categoryDto) {
        if (!StringUtils.hasText(categoryDto.getName())) {
            throw new SystemException(AppHttpCodeEnum.CATEGORY_NAME_ISNULL);
        }
        if (!StringUtils.hasText(categoryDto.getDescription())) {
            throw new SystemException(AppHttpCodeEnum.CATEGORY_DESCRIPTION_ISNULL);
        }
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Category::getId,categoryDto.getId())
                .set(Category::getName,categoryDto.getName())
                .set(Category::getDescription,categoryDto.getDescription())
                .set(Category::getStatus,categoryDto.getStatus());
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<?> deleteCategory(List<Long> ids) {
        removeBatchByIds(ids);
        return ResponseResult.okResult();
    }
}
